package com.homevip.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.homevip.core.page.Page;
import com.homevip.core.page.Pageable;
import com.homevip.core.util.FastDate;
import com.homevip.user.dao.UserRoleDao;
import com.homevip.user.entity.User_role;
import com.homevip.user.service.RoleService;
import com.homevip.util.SqlHelper;
import com.homevip.util.StringUtil;

/**
 * 用户角色Service实现类
 * @author panpan
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService{

	@Autowired
	UserRoleDao userRoleDao;
	
	@Override
	public User_role getUserRoleById(int id) {
		User_role user_role = userRoleDao.getById(id);
		return user_role;
	}

	@Override
	@Transactional
	public void saveUserRole(User_role userRole) {
		if (0 == userRole.getId()){
			userRole.setCreatedate(new FastDate().toText());
			userRole.setLastdate(new FastDate().toText());
			userRoleDao.save(userRole);
		}else{
			userRole.setLastdate(new FastDate().toText());
			
			userRoleDao.update(userRole);
		}	
	}

	@Override
	@Transactional
	public void deleteUserRole(User_role userRole) {
		userRoleDao.delete(userRole);
	}

	@Override
	public Page<User_role> findUserRole(String keyword, Integer status, Pageable pageable) {
		Map<String, Object> params = new HashMap<String, Object>();
		Page<User_role> page = null; 

		if(null != status && status > 0){
			params.put("status", status);
		}
			
		if(!StringUtil.isEmpty(keyword)) {
			keyword = SqlHelper.toJpqlParamLike(keyword);
		}

		page = userRoleDao.findByAndLike(params, "cname", keyword, pageable);

		return page;
	}
	
	@Override
	public List<User_role> findUserRole(String keyword, Integer status) {
		Map<String, Object> params = new HashMap<String, Object>();

		if(null != status && status > 0){
			params.put("status", status);
		}
			
		if(!StringUtil.isEmpty(keyword)) {
			keyword = SqlHelper.toJpqlParamLike(keyword);
		}

		return userRoleDao.findByAndLike(params, "cname", keyword);
	}

}
